/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.annotation.Generated;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Alejandro
 */
@Entity
@Table(name = "RS_CATEGORIES")
@NamedQueries({
    @NamedQuery(name = "Category.findAllChildren", query = "SELECT r FROM Category r WHERE r.parentCategory IS NOT NULL AND r.status='1'"),
    @NamedQuery(name = "Category.findChildrenForParent", query = "SELECT r FROM Category r WHERE r.parentCategory IS NULL AND r.status='1'"),
    @NamedQuery(name = "Category.findByIdParentCategory", query = "SELECT r FROM Category r  WHERE r.parentCategory = :parentCategory AND r.status='1'"),
    @NamedQuery(name = "Category.findAll", query = "SELECT r FROM Category r"),
    @NamedQuery(name = "Category.findByIdCategory", query = "SELECT r FROM Category r WHERE r.idCategory = :idCategory"),
    @NamedQuery(name = "Category.findByName", query = "SELECT r FROM Category r WHERE r.name = :name"),
    @NamedQuery(name = "Category.findByIdCatsuper", query = "SELECT r FROM Category r WHERE r.parentCategory = :parentCategory")})
public class Category implements Serializable, Comparable<Category> {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @GeneratedValue
    @Column(name = "ID_CATEGORY")
    private Integer idCategory;
    @Basic(optional = false)
    @NotNull
    @Column(name = "NAME")
    private String name;
    @OneToOne
    @JoinColumn(name = "PARENT_ID_CATEGORY")
    private Category parentCategory;
    @Basic(optional = false)
    @NotNull
    @Column(name = "STATUS")
    private Integer status;

    public Category() {
    }

    public Category(String name) {
        this();
        this.name = name;
        this.status = 1;
    }
    
    public Category(String name, Category parentCategory) {
        this(name);
        this.parentCategory = parentCategory;
    }

    public Integer getIdCategory() {
        return idCategory;
    }

    public void setIdCategory(Integer idCategory) {
        this.idCategory = idCategory;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category getParentCategory() {
        return parentCategory;
    }

    public void setParentCategory(Category parentCategory) {
        this.parentCategory = parentCategory;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idCategory != null ? idCategory.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Category)) {
            return false;
        }
        Category other = (Category) object;
        if ((this.idCategory == null && other.idCategory != null) || (this.idCategory != null && !this.idCategory.equals(other.idCategory))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return idCategory + " - "+ name;
    }

    @Override
    public int compareTo(Category t) {
       return name.compareTo(t.getName());
    }
    
    
}
